package com.ce.framework.util.node;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import com.ce.framework.datamaintain.logic.ISQLMapper;

/**
 * 嫦娥软件有限公司 版权所有 2013
 * 创建人： andy
 * 创建时间： 2013-9-15 下午02:52:17
 * 功能描述：构造用于json数据格式显示的树节点
 * ==================================
 * 修改历史
 * 修改人        修改时间      修改位置（函数名）
 *
 * ==================================
 */
public class JsonTreeNode {
	
	/**
	 * 节点的唯一ID
	 */
	private String id;
	/**
	 * 节点状态，打开：open  关闭 ：'closed'
	 */
	private String state;
	/**
	 * 节点名称
	 */
	private String text;
	/**
	 * 节点父ID，用来生成JSON Tree的标记
	 */
	private String pid;
	/**
	 * 图片css名称
	 */
	private String iconCls;

	/**
	 * 节点属性
	 */
	private Object attributes;
	/**
	 * 子节点
	 */
	private List<JsonTreeNode> children;
	
	private boolean checked;
	
    public boolean isChecked() {
		return checked;
	}
	public void setChecked(boolean checked) {
		this.checked = checked;
	}
	public JsonTreeNode() {
	}
	
    public JsonTreeNode(String id, String text) {
		super();
		this.id = id;
		this.text = text;
	}

	public JsonTreeNode(String id, String text, String iconCls) {
		super();
		this.id = id;
		this.text = text;
		this.iconCls = iconCls;
	}

	public JsonTreeNode(String id, String text, String iconCls, String state) {
		super();
		this.id = id;
		this.state = state;
		this.text = text;
		this.iconCls = iconCls;
	}

	public JsonTreeNode(String id, String text, String iconCls, String state,String pid) {
		super();
		this.id = id;
		this.state = state;
		this.text = text;
		this.iconCls = iconCls;
		this.pid = pid;
	}

	public static ISQLMapper mapper = new ISQLMapper(){
		public Object fromSQLQuery(ResultSet rs, int num) {
			JsonTreeNode n = new JsonTreeNode();
			try {
				n.setId(rs.getString(1));
				n.setText(rs.getString(2));
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return n;
		}
	};

	public static ISQLMapper mapper3 = new ISQLMapper(){
		public Object fromSQLQuery(ResultSet rs, int num) {
			JsonTreeNode n = new JsonTreeNode();
			try {
				n.setId(rs.getString(1));
				n.setText(rs.getString(2));
				n.pid = rs.getString(3);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return n;
		}
	};
	
	/**
	* 开发人：andy
	* 开发日期: 2012-6-13  上午10:11:30
	* 功能描述: 根据指定的根节点ID，从LIST中检索出该节点的Json Tree模型，
	* 			如果rootId在list中找不到，则使用list中第一个元素做为根节点
	* 方法的参数和返回值: 
	* @param list
	* @param rootId
	* @return
	*/
	public static JsonTreeNode createTree(List<JsonTreeNode> list,String rootId){
		LinkedHashMap<String,JsonTreeNode> map = new LinkedHashMap<String,JsonTreeNode>();
		for(JsonTreeNode n:list){
			map.put(n.id, n);
		}
		JsonTreeNode root = map.get(rootId);
		if(root==null){
			root = list.isEmpty()?null:list.get(0);
		}
		JsonTreeNode pn = null;
		for(JsonTreeNode n:list){
			if(map.containsKey(n.pid)){
				pn=map.get(n.pid);
				if(pn.getChildren()==null){
					pn.setChildren(new ArrayList<JsonTreeNode>());
				}
				pn.getChildren().add(n);
			}
		}
		return root;
	}

	//临时节点（显示正在加载中）
	public static List<JsonTreeNode> TMP_LOADING_LIST = getTempChild();
	public static List<JsonTreeNode> getTempChild(){
		List<JsonTreeNode> list = new ArrayList<JsonTreeNode>(1);
		JsonTreeNode n = new JsonTreeNode();
		n.setId("tmp_loading");
		n.setText("正在加载数据...");
		n.setIconCls("icon-empty");
		list.add(n);
		return list;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public Object getAttributes() {
		return attributes;
	}
	public void setAttributes(Object attributes) {
		this.attributes = attributes;
	}
	public List<JsonTreeNode> getChildren() {
		return children;
	}
	public void setChildren(List<JsonTreeNode> children) {
		this.children = children;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}

	public String getIconCls() {
		return iconCls;
	}
	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}
	
	

}
